package com.wynn.controller;

import com.wynn.pojo.Bom;
import com.wynn.pojo.Product;
import com.wynn.pojo.Result;
import com.wynn.serviceFeign.ProductClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductClient productClient;

    @GetMapping("selectAll")
    public Result selectAll(HttpServletResponse response) throws IOException {
        Result result = new Result();
        try{
            result = productClient.selectAll();
            result.setCode("200");
        }catch (Exception e){
            log.warn("加载产品失败");
            result.setMessage("加载产品失败");
            result.setCode("503");
        }
        return result;
    }

    @PostMapping("add")
    @PreAuthorize("hasRole('admin')")
    public Result add(@RequestBody Product product){
        Result result = new Result();
        try{
            productClient.add(product);
            result.setCode("200");
            result.setMessage("产品添加成功");
        }catch (Exception e){
            log.warn("添加产品失败");
            result.setMessage("添加产品失败");
            result.setCode("503");
        }
        return result;
    }

    @GetMapping("delete")
    @PreAuthorize("hasRole('admin')")
    public Result delete(int id){
        Result result = new Result();

        try{
            productClient.delete(id);
            result.setCode("200");
            result.setMessage("删除成功");
        }catch (Exception e){
            log.warn("删除产品失败");
            result.setCode("503");
            result.setMessage("删除失败");
        }

        return result;
    }

    @PostMapping("edit")
    @PreAuthorize("hasRole('admin')")
    public Result edit(@RequestBody Product product){
        Result result = new Result();

        try{
            productClient.edit(product);
            result.setCode("200");
            result.setMessage("修改成功");
        }catch (Exception e){
            log.warn("修改产品失败");
            result.setCode("503");
            result.setMessage("修改失败");
        }
        return result;
    }

    @GetMapping("search")
    public Result search(String productModel){
        Result result = new Result();

        try{
            result = productClient.search(productModel);
            result.setCode("200");
            result.setMessage("查询成功");
        }catch (Exception e){
            log.warn("查询产品失败");
            result.setCode("503");
            result.setMessage("查询失败");
        }
        return result;
    }

    @GetMapping("bom")
    public Result bom(){
        Result result = new Result();
        try {
            List<Bom> bomList = productClient.bom();
            result.setCode("200");
            result.setData(bomList);
        }catch (Exception e){
            log.warn("查询Bom失败");
            result.setCode("503");
            result.setMessage("查询bom清单失败");
        }
        return result;
    }

    @PostMapping("bomAdd")
    @PreAuthorize("hasRole('admin')")
    public Result bomAdd(@RequestBody Bom bom){
        Result result = new Result();
        try{
            productClient.bomAdd(bom);
            result.setCode("200");
            result.setMessage("添加成功");
        }catch (Exception e){
            log.warn("添加Bom失败");
            result.setCode("503");
            result.setMessage("添加失败");
        }
        return result;
    }

    @GetMapping("deleteBom")
    @PreAuthorize("hasRole('admin')")
    public Result deleteBom(int id){
        Result result = new Result();
        try{
            productClient.deleteBom(id);
            result.setCode("200");
            result.setMessage("删除成功");
        }catch (Exception e){
            log.warn("删除Bom失败");
            result.setCode("503");
            result.setMessage("删除失败");
        }
        return result;
    }

    @PostMapping("editBom")
    @PreAuthorize("hasRole('admin')")
    public Result editBom(@RequestBody Bom bom){
        Result result = new Result();
        try {
            productClient.editBom(bom);
            result.setCode("200");
            result.setMessage("修改成功");
        }catch (Exception e){
            log.warn("修改Bom失败");
            result.setCode("503");
            result.setMessage("修改失败");
        }
        return result;
    }

    @GetMapping("searchBom")
    public Result searchBom(String name){
        Result result = new Result();
        try {
            List<Bom> bomList = productClient.searchBom(name);
            result.setCode("200");
            result.setData(bomList);
            result.setMessage("查询成功");
        }catch (Exception e){
            log.warn("搜索Bom失败");
            result.setCode("503");
            result.setMessage("查询失败");
        }
        return result;
    }

}
